package com.joshondesign.sdljava.demo;

import com.joshondesign.sdljava.SDLUtil;
import com.joshondesign.sdljava.SDL_Surface;
import com.joshondesign.sdljava.gfx.*;

/**
 * Created by IntelliJ IDEA.
 * User: josh
 * Date: 9/9/11
 * Time: 11:20 AM
 * To change this template use File | Settings | File Templates.
 */
public class SimpleTranslate implements SDLUtil.InitCallback {
    public static void main(String ... args) {
        SDLUtil.init(new SimpleTranslate());
    }

    int currentLayer = 0;
    public void callback() {
        try {
            Util.standard_init();
            SDL_Surface surface = Util.createResizableWindow(800, 600);
            final Scene scene = new Scene(surface);
            scene.setBackgroundFill(SColor.BLACK);


            final Layer layer1 = new Layer(surface.getFormat());
            layer1.add(new Rect(0,0,800,600,SColor.RED));
            layer1.add(new Text(300,300,"Slide 1"));
            scene.add(layer1);

            Layer layer2 = new Layer(surface.getFormat());
            layer2.add(new Rect(0,0,800,600,SColor.GREEN));
            layer2.add(new Text(300,300,"Slide 2"));
            scene.add(layer2);
            layer2.x = -1000;

            Layer layer3 = new Layer(surface.getFormat());
            layer3.add(new Rect(0,0,800,600,SColor.BLUE));
            layer3.add(new Text(300,300,"Slide 3"));
            layer3.x = -1000;
            scene.add(layer3);


            final Layer[] layers = {layer1,layer2,layer3};

            final double duration = 0.6;
            Callback goNext = new Callback() {
                public void call(Object o) {
                    scene.animate(new PropAnimation(layers[currentLayer % layers.length], "x", 0, -800, duration));
                    scene.animate(new PropAnimation(layers[(currentLayer + 1) % layers.length], "x", 800, 0, duration));
                    currentLayer++;
                }
            };
            scene.listen(Trigger.MousePressed().setCallback(goNext));
            scene.listen(Trigger.KeyPressed(Trigger.LeftArrow).setRepeat(false).setCallback(goNext));
            scene.listen(Trigger.KeyPressed(Trigger.RightArrow).setRepeat(false).setCallback(new Callback() {
                public void call(Object o) {
                    scene.animate(new PropAnimation(layers[currentLayer % layers.length],
                            "x", 0, 800, duration));
                    scene.animate(new PropAnimation(layers[(currentLayer - 1 + layers.length) % layers.length],
                            "x", -800, 0, duration));
                    currentLayer--;
                    if(currentLayer < 0) {
                        currentLayer += layers.length;
                    }
                }
            }));

            scene.start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
